/*
 * Project Name: jmax
 * File Name: SessionDao.java
 * Copyright: Copyright(C) 1985-2015 ZKTeco Inc. All rights reserved.
 */
package com.xdtech.shiro.dao;

import java.io.Serializable;

import org.apache.shiro.session.Session;
import org.apache.shiro.session.mgt.ValidatingSession;
import org.apache.shiro.session.mgt.eis.CachingSessionDAO;
import org.springframework.beans.factory.annotation.Autowired;

import com.xdtech.shiro.ShiroConstants;
import com.xdtech.shiro.model.UserSession;
import com.xdtech.shiro.service.UserSessionService;
import com.xdtech.shiro.session.mgt.SysSession;
import com.xdtech.shiro.session.mgt.SysSessionFactory;

/**
 * TODO 一句话功能简述，请确保和下面的block tags之间保留一行空行
 * <p>
 * TODO 功能详细描述，若不需要请连同上面的p标签一起删除
 * 
 * @author <a href="max.zheng@zkteco.com">郑志雄</>
 * @version TODO 添加版本
 * @see 相关类或方法，不需要请删除此行
 * @since 2015-2-6 下午3:12:06
 */
public class SessionDao extends CachingSessionDAO {

	@Autowired
	private UserSessionService userSessionService;
	@Autowired
	private SysSessionFactory sessionFactory;
	/**
	 * 
	 * @description 删除会话；当会话过期/会话停止（如用户退出时）会调用  
	 * @author max.zheng
	 * @create 2015-2-8下午3:33:43
	 * @modified by
	 * @param session
	 */
	@Override
	protected void doDelete(Session session) {
		SysSession sysSession = (SysSession) session;
		// 定时任务删除的此时就不删除了
		if (sysSession.getAttribute(ShiroConstants.ONLY_CLEAR_CACHE) == null) {
			try {
				userSessionService.offline(String.valueOf(sysSession.getId()));
			} catch (Exception e) {
				// 即使删除失败也无所谓
				e.printStackTrace();
			}
		}

	}

	/**
	 * @description 更新会话；如更新会话最后访问时间/停止会话/设置超时时间/设置移除属性等会调用  
	 * @author max.zheng
	 * @create 2015-2-8下午2:43:16
	 * @modified by
	 * @param session
	 */
	@Override
	protected void doUpdate(Session session) {
		SysSession sysSession = (SysSession) session;
		if (session instanceof ValidatingSession
				&& !((ValidatingSession) session).isValid()) {
			return; // 如果会话过期/停止 没必要再更新了
		}
//		UserSession userSession = UserSession.fromUserSession(sysSession);
//		System.out.println("doUpdate"+sysSession.getId());
//		userSessionService.saveOrUpdate(userSession);

	}

	/**
	 * @description//如DefaultSessionManager在创建完session后会调用该方法；如保存到关系数据库/文件系统/NoSQL数据库；即可以实现会话的持久化；返回会话ID；主要此处返回的ID.equals(session.getId())；  
	 * @author max.zheng
	 * @create 2015-2-8下午2:43:16
	 * @modified by
	 * @param session
	 * @return
	 */
	@Override
	protected Serializable doCreate(Session session) {
		Serializable sessionId = generateSessionId(session);
		assignSessionId(session, sessionId);
		SysSession sysSession = (SysSession) session;

		UserSession userSession = UserSession.fromUserSession(sysSession);
		userSessionService.saveOrUpdate(userSession);
		return sysSession.getId();
	}

	/**
	 * @description/根据会话ID获取会话  
	 * @author max.zheng
	 * @create 2015-2-8下午2:45:49
	 * @modified by
	 * @param sessionId
	 * @return
	 */
	@Override
	protected Session doReadSession(Serializable sessionId) {
		UserSession userSession = userSessionService.findOneById(sessionId
				.toString());
		if (userSession == null)
			return null;
		return sessionFactory.createSession(userSession);
	}
}
